<?php
    /*
        @desc main input of the system framework
    */

    define( 'DEBUG', false );

    //output encoding
    if ( defined( 'DEBUG' ) && DEBUG ) {
        ob_start( );
    } else {
        if ( ini_get( 'zlib.output_compression' ) ) {
            if ( ini_get( 'zlib.output_compression_level' ) != 5 ) {
                ini_set( 'zlib.output_compression_level', '5' );
            }

            ob_start( );
        } else {
            if ( strstr( $_SERVER['HTTP_ACCEPT_ENCODING'], "gzip" ) ) {
                ob_start( "ob_gzhandler" );
            } else {
                ob_start( );
            }
        }
    }

    if ( defined( 'DEBUG' ) && DEBUG) {
        //set error reporting level
        error_reporting( E_ALL );
    } else {
        //set error reporting level
        error_reporting( 0 );
    }

    //main input of system page rendering
    //init path vars
    define( 'SYS', str_replace( '\\', '/', dirname( __FILE__ ) ) . '/' );
    define( 'SYS_APP', SYS . '_app/site/' );
    define( 'SYS_LIB', SYS . '_lib/' );
    define( 'SYS_TEMPLATE', SYS . '_app/template/' );
    define( 'SYS_CONFIG', SYS . '_app/config/' );
    define( 'SYS_TEMP', SYS . '_temp/' );
    define( 'SYS_TEMPLATE_C', SYS_TEMP . 'template/compiled/' );
    define( 'SYS_TEMPLATE_CACHE', SYS_TEMP . 'template/cache/' );
    
    //load booting functions
    require_once( SYS_LIB . '__init__.php' );

    //preload system settings
    require_once( SYS_CONFIG . '__init__.php' );

    //loading system configuration
    load_config( 'System' );

    //modify session name
    //default as fr262144
    session_name( isset( $system['session'] ) ? $system['session'] : 'fr262144' );
    //anti session robbery
    unset( $_GET[session_name()] );
    unset( $_POST[session_name()] );
    //set timezone
    date_default_timezone_set( 'PRC' );
    //set charset
    header('Content-Type: text/html; charset=utf-8');

    //defining URL constants
    define( 'URL', 'http://' . $_SERVER['HTTP_HOST']
        . substr( $_SERVER['SCRIPT_NAME'], 0, 
            strpos( $_SERVER['SCRIPT_NAME'], 'index.php' ) ) );
    define( 'URL_BASE',
        URL . ( isset( $system['rewrite'] ) && $system['rewrite'] ? '' : 'index.php/' ) );
    define( 'URL_CSS', URL . 'public/css/' );
    define( 'URL_JS', URL . 'public/js/' );
    define( 'URL_IMAGE', URL . 'public/images/' );
    define( 'URL_SWF', URL . 'public/swf/' );

    //load libs
    load_lib( 'error' );
    load_lib( 'db' );
    load_lib( 'quickskin' );
    load_lib( 'core' );
    load_lib( 'dispatcher' );

    //dispatch to application，
    Dispatcher::dispatch( );